package com.jimikongjian.controller;

import com.jimikongjian.models.Comment;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/3/4.
 */
@RestController("commentController")
@RequestMapping(value = "/v1")
public class CommentController {

    @Autowired
    @Qualifier("commentService")
    private ICommentService commentService;


    /**
     * 创建评论
     * @param comment
     * @return
     */
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage createComment(@RequestBody Comment comment){
        return commentService.createComment(comment);
    }


    /**
     * 查询某一篇文章的评论
     * @param articleId
     * @param start
     * @param length
     * @return
     */
    @RequestMapping(value = "/comment", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectComment(@RequestParam(name = "articleId", defaultValue = "-1") Integer articleId,
                                         @RequestParam(name = "commentParentId", defaultValue = "-1") Integer commentParentId,
                                         @RequestParam(name = "commentId", defaultValue = "-1") Integer commentId,
                                         @RequestParam(name = "start", defaultValue = "0") Integer start,
                                         @RequestParam(name = "length",defaultValue = "10") Integer length){
        if (commentParentId > 0){
            return commentService.selectCommentListByArticleIdAndCommentParentId(articleId, commentParentId, start, length);
        }
        if (commentId > 0){
            return commentService.selectCommentListByArticleIdAndCommentId(articleId, commentId, start, length);
        }
        return commentService.selectComment(articleId, start, length);
    }
}
